<template>
    <view :class="uiStyle == 1 ? '' : 'uiStyle'">
        <!-- 车牌管理页面 -->
		<view class="head-block" v-if="weixin == 2">
		     <view class="nav-title" :style="'top:10px'"></view>
			 <!-- 返回 -->
			 <view class="icon" @tap="goMy" :style="'top:6px'">
			      <van-icon name="arrow-left" color="#333" size="45rpx" />
			  </view>
		 </view>

        <view style="overflow: scroll; height: 80vh">
            <!-- 车牌循环开始-蓝绿交替 -->
            <view
                @tap="gotoDetail"
                :data-ocrCheckStatus="item.ocrCheckStatus"
                :data-url="'/pages/licenseIdentify/licenseIdentify?id=' + item.plateNo"
                class="top-cards"
                v-for="(item, index) in plateNos"
                :key="index"
            >
                <view class="license-area">
                    <view class="license-card">
                        <view class="confirm-tag" v-if="item.ocrCheckStatus == 1" :data-data="item" :data-id="index">已认证</view>
                        <view class="noconfirm-tag" v-else :data-data="item" :data-id="index">未认证</view>
                        <view class="card-inner">{{ item.plateNo }}</view>
                        <view v-if="index == 0" class="default-tag">默认</view>
                        <view v-else class="set-tag" @tap.stop.prevent="setDefault" :data-data="item" :data-id="index">设为默认</view>
                    </view>
                    <view class="del-icon" @tap.stop.prevent="delLicense" :data-data="item" :data-id="index">
                        <image src="../../static/assets/img/shanchu.png"></image>
                    </view>
                </view>
            </view>
            <view class="fix-btm">
                <view class="btn" @tap="gotoAdd">+ 添加车牌</view>
                <view class="tip-text">提示：暂时不支持临牌车辆</view>
            </view>
        </view>
        <van-dialog id="van-dialog" confirm-button-color="#00BCAB" />
    </view>
</template>

<script>
// pages/licenseManage/licenseManage.js
import util from './../../utils/util.js';
import api from './../../utils/api.js';
import { Dialog } from 'vant';
export default {
    data() {
        return {
            username: '',
            //获取到就传死
            phoneNumber: '',
            //获取到就传死
            plateNos: '',
			uiStyle:1,
			weixin:1,
            //动态变更
            url: '' //头像传空  就不会修改
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
		this.uiStyle = uni.getStorageSync('uiStyle')
		this.weixin = uni.getStorageSync('weixin')
        // 处理关于分享的用户unionId
        if (options.shareId && options.shareId != '') {
            uni.setStorageSync('shareId', options.shareId);
        }

        if (options.activityCode && options.activityCode != '') {
            uni.setStorageSync('activityCode', options.activityCode);
        }

        uni.showLoading({
            title: '加载中'
        });
        this.getUserCar();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        uni.showLoading({
            title: '加载中'
        });
        this.getUserCar();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    // onShareAppMessage: function () {
    // }
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */ methods: {
		 goMy() {
		     uni.navigateBack({
		         delta: 1
		     });
		 },
        // 跳去加车牌页面
        gotoAdd() {
            uni.navigateTo({
                url: '../addLicense/addLicense'
            });
        },

        gotoDetail(event) {
            if (event.currentTarget.dataset.ocrCheckStatus == 1) {
                return;
            }

            if (event.currentTarget.dataset.url) {
                uni.navigateTo({
                    url: event.currentTarget.dataset.url
                });
            } else {
                uni.showModal({
                    title: '',
                    content: '该板块正在建设中',
                    showCancel: false,
                    confirmText: '我知道了',
                    confirmColor: '#FFA000',

                    success(res) {
                        if (res.confirm) {
                            // console.log('用户点击确定')
                        }
                    }
                });
            }
        },

        // 设为默认--调用修改接口--进行顺序排列；修改后再次调用获取个人资料页刷新
        setDefault(e) {
            let that = this;
            let i = e.currentTarget.dataset.id;
            let curLicense = e.currentTarget.dataset.data;
            Dialog.confirm({
                context: this,
                message: '确定将车牌' + curLicense.plateNo + '设置为默认车牌？'
            })
                .then(() => {
                    // on confirm
                    that.plateNos.splice(i, 1);
                    that.plateNos.unshift(curLicense);
                    that.setDefaultPlateNo(curLicense.plateNo);
                })
                .catch(() => {
                    // on cancel
                });
        },

        setDefaultPlateNo(plateNo) {
            let that = this;
            util.request(api.setDefaultPlateNo + '?plateNo=' + plateNo, 'get') //
                .then((res) => {
                    if (res.code == 0) {
                        console.log('设置默认成功！');
                        uni.showToast({
                            title: '设置默认成功！',
                            icon: 'success',
                            duration: 1000
                        });
                        that.getUserCar();
                    }
                });
        },

        deletePlateNo(i, plateNo) {
            let that = this;
            util.request(api.deletePlateNo + '?plateNo=' + plateNo, 'get') //
                .then((res) => {
                    if (res.code == 0) {
                        uni.hideLoading();
                        console.log('删除成功！');
                        that.plateNos.splice(i, 1);
                        this.setData({
                            plateNos: that.plateNos || []
                        });
                        console.log('··············',that.plateNos); 
						var _plateNos1=[];
                        var _plateNos="";
                        for(var i=0;i<that.plateNos.length;i++){
                          _plateNos1.push(that.plateNos[i].plateNo)
                        }
                        _plateNos=_plateNos1.join(",");
                        wx.setStorage({
                          key: "plateNos",
                          data: _plateNos
                        }); //用户车牌号
						if(that.plateNos.length == 0){
							uni.removeStorageSync('plateNos')
						}
                        uni.showToast({
                            title: '删除成功',
                            icon: 'success',
                            duration: 1000
                        });
                    } else {
                        uni.hideLoading();
                        uni.showToast({
                            title: res.msg,
                            duration: 1000
                        });
                    }
                });
        },

        // 删除车牌
        delLicense(e) {
            let that = this;
            let i = e.currentTarget.dataset.id;
            let delLicense = e.currentTarget.dataset.data;
            Dialog.confirm({
                context: this,
                message: '确定删除车牌' + delLicense.plateNo + '？'
            })
                .then(() => {
                    // on confirm
                    uni.showLoading({
                        title: '删除中，请稍后！'
                    });
                    that.deletePlateNo(i, delLicense.plateNo);
                })
                .catch(() => {
                    // on cancel
                });
        },

        // 提交修改
        setupdateUser() {
            const identityId = uni.getStorageSync('identityId');
            var _plates = [];

            for (var i = 0; i < this.plateNos.length; i++) {
                _plates.push(this.plateNos[i].plateNo);
            }

            console.log(_plates);
            const param = {
                loginName: uni.getStorageSync('phoneNumber'),
                name: this.username,
                url: this.url,
                plates: _plates,
                identityId: identityId
            };
            util.request(api.updateUser, param, 'POST').then((res) => {
                if (res.code == 0) {
                    uni.showToast({
                        title: '修改成功',
                        icon: 'none'
                    });
                    this.getUserCar();
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            });
        },

        //获取个人资料
        getUserCar() {
            uni.hideLoading();
            let value = uni.getStorageSync('unionId');
            util.request(api.findUserCar, 'get') // + "?personUnionId=" + value
                .then((res) => {
                    if (res.code == 0) {
                        this.setData({
                            plateNos: res.data || []
                        }); 
						console.log(res.data)
						var _plateNos1=[];
                        var _plateNos="";
                        for(var i=0;i<res.data.length;i++){
                          _plateNos1.push(res.data[i].plateNo)
                        }
                        _plateNos=_plateNos1.join(",");
                        wx.setStorage({
                          key: "plateNos",
                          data: _plateNos
                        }); //用户车牌号
                    }
                });
        }
    }
};
</script>
<style>
@import './licenseManage.css';
</style>
